//
//  TransNextViewController.m
//  MyFunctionDemo
//
//  Created by Ting Han on 2017/9/22.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "TransNextViewController.h"
#import "UIView+SetRect.h"
#import "DismissAnimator.h"

@interface TransNextViewController ()<UIViewControllerTransitioningDelegate>

//交互式转场，只在当前控制器有效
@property (nonatomic, strong) UIPercentDrivenInteractiveTransition*
interactiveTransition;

@end

@implementation TransNextViewController
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.transitioningDelegate = self;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if (self.transitioningDelegate == self) {
        self.transitioningDelegate = nil;
    }
    
}
- (void)setupDataSource{
    self.title = NSStringFromClass([self class]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:button];
    button.backgroundColor = [[UIColor brownColor]colorWithAlphaComponent:0.3];
    [button setTitle:@"dismiss" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, kWidth(150), kWidth(80));
    [button addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    button.center = CGPointMake(self.contentView.centerX, kWidth(90));
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, kWidth(200), kWidth(300), 0)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.textColor = [UIColor brownColor];
    [self.contentView addSubview:label];
    label.text = @"当前页做了dismiss的交互转场动画\n       pop转场动画，添加了交互式转场，采用UIPercentDrivenInteractiveTransition,配合自定义的转场动画,能实现手势滑动返回效果。\n        当自定义navigationBar的返回ButtonItem的时候，返回手势会失效，需要在navigationController中把self.navigationController.interactivePopGestureRecognizer.delegate = self设置成自己";
    [label sizeToFit];
    
    //添加手势
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] init];
    [pan addTarget:self action:@selector(panGestureRecognizerAction:)];
    [self.contentView addGestureRecognizer:pan];
    
}
- (void)didClickBtn:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --- present & dismiss
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)pan{
    //产生百分比
    CGFloat process = [pan translationInView:self.view].y / ([UIScreen mainScreen].bounds.size.height);
    process = MIN(1.0,(MAX(0.0, process)));
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.interactiveTransition = [UIPercentDrivenInteractiveTransition new];
 
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else if (pan.state == UIGestureRecognizerStateChanged){
        [self.interactiveTransition updateInteractiveTransition:process];
    }else if (pan.state == UIGestureRecognizerStateEnded
              || pan.state == UIGestureRecognizerStateCancelled){
        if (process > 0.3) {
            [ self.interactiveTransition finishInteractiveTransition];
        }else{
            [ self.interactiveTransition cancelInteractiveTransition];
        }
        self.interactiveTransition = nil;
    }
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    DismissAnimator* ani = [[DismissAnimator alloc] init];
    return ani;
    
}

-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator{
    if ([animator isKindOfClass:[DismissAnimator class]]) {
        return self.interactiveTransition;
    }
    return nil;
}
@end
